/*


	jQuery plugin: Simple Tabs
	Author: Ian at Grand Union [ianh@thegrandunion.com]


	What?
	=====
	Super simple tabs functionality.


	How?
	====
	HTML:
	
		<ul class="tabs">
			<li><a href="#tab-1">Tab 1</a></li>
			<li><a href="#tab-2">Tab 2</a></li>
			<li><a href="#tab-3">Tab 3</a></li>
		</ul>
	
	JS:
	
		$('.tabs').simpleTabs();


	Options
	=======
	show: The function to execute when displaying a tab.
	hide: The function to execute when hiding a tab.
	activeClass: The CSS class to add to tabs as they become active.
	
	
	Dependencies
	============
	- jQuery 1.4 or higher (jquery.com)


*/
(function($){

	$.fn.simpleTabs = function(settings){

		var G = {
			config : {
				show : function($el){ $el.show(); },
				hide : function($el){ $el.hide(); },
				activeClass: 'selected'
			},
			$this : this
		};
		
		if(settings){ $.extend(G.config, settings); }


		function init()
		{
			var sID, href = '';

			// Loop through each tab navigation.
			G.$this.each(function(i){
				// Loop through each link.
				$(this).find('li').each(function(y)
				{
					/*
						Why is the Href retrieval so convoluted? Avoids a weird bug in IE 6 (surprise!) where the href value 
						can return the entire URL "http://www.example.com/directory/this-pahe.html#tab-1" rather than just "#tab-1".
					*/
					href = $(this).find('a').attr('href');
					href = href.substr(href.lastIndexOf('#'));
					if(y>0){ // Greater than zero as we won't hide the first one.
						sID += ','+href+',';
					}
					else{
						$(this).addClass(G.config.activeClass);
					}
					$(this).click(function(e){
						e.preventDefault();
						switchTabs(this);
					});
				});
			});

			$(sID).hide();
		}
		
		
		function switchTabs(el)
		{
			var $sel = $(el).parent().find('.'+G.config.activeClass),
				selHref = $sel.find('a').attr('href'),
				elHref = $(el).find('a').attr('href');
				
			selHref = selHref.substr(selHref.lastIndexOf('#'));
			elHref = elHref.substr(elHref.lastIndexOf('#'));

			// Hide the currently selected tab.
			G.config.hide($(selHref));
			$sel.removeClass(G.config.activeClass);

			// Show the new one.
			G.config.show($(elHref));
			$(el).addClass(G.config.activeClass);
		}
		

		init();
		return this;
	
	};

})(jQuery);